﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crosland.Offline.Logic;
using Crosland.Offline.Core;

namespace Crosland.Offline.WinForm
{
    public partial class UCBusquedaMateriales : UserControl
    {

        private List<string> cogidos = new List<string>();
        public List<string> Codigos
        {
            get { return cogidos; }
            set { cogidos = value; }
        }

        public event EventHandler Aceptar;


        public UCBusquedaMateriales()
        {
            InitializeComponent();
        }

        private void UCBusquedaMateriales_Load(object sender, EventArgs e)
        {
            dgwListaMaterial.AutoGenerateColumns = false;
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            MaterialLogic ma = new MaterialLogic();

            List<Material> lstMaterial = new List<Material>();
            lstMaterial = ma.GetByCriteria(txtMATNR.Text, txtMAKTX.Text, "0002", txtMarca.Text, txtModelo.Text);
            dgwListaMaterial.DataSource = lstMaterial;
            dgwListaMaterial.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void dgwListaMaterial_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                List<Material> lstMat = new List<Material>();

                Material objMat = new Material();
                lstMat = (List<Material>)dgwListaMaterial.DataSource;
                objMat = lstMat[e.RowIndex];
                lstMatAñadido.Items.Add(objMat.Matnr.Trim() + " - " + objMat.Maktx.Trim());
            }
        }

        private void txtMATNR_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void lstMatAñadido_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = this.lstMatAñadido.IndexFromPoint(e.Location);
            lstMatAñadido.Items.RemoveAt(index);
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            lstMatAñadido.Items.Clear();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (lstMatAñadido.Items.Count > 0)
            {
                foreach (object s in this.lstMatAñadido.Items)
                {
                    string[] material = ((string)s).Split('-');
                    this.Codigos.Add(material[0].Trim());
                }

                this.Aceptar(sender, e);
            }

        }

    }
}
